//
//  CarSafeRangesViewController.m
//  carcareIOS
//
//  Created by ileo on 15/7/6.
//  Copyright (c) 2015年 baozun. All rights reserved.
//

#import "CarFenceViewController.h"
#import "CarFenceDetailViewController.h"
#import "CarFenceMgr.h"
#import "CarFenceCell.h"

#import "AISystemInfo.h"

#import "TipsFactory.h"

@interface CarFenceViewController ()

@end

@implementation CarFenceViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [CommentHelper useFunction:Use_CarFence];
    
    self.title = @"安全区域";
    __weak __typeof(self) wself = self;
    [self setupNavRightImageName:@"addnew_icon" action:^{
        CarFenceDetailViewController *vc = [[[CarFenceDetailViewController alloc] init] setupNavBack];
        [wself.navigationController pushViewController:vc animated:YES];
    }];

//    if ([AISystemInfo sharedInstance].shouldShowCarFenceNewTips) {
//        UIWindow *mainWindow = [UIApplication sharedApplication].delegate.window;
//        UIView *view = [Factory viewWithFrame:mainWindow.bounds bgColor:[[UIColor blackColor] colorWithAlphaComponent:0.8] onView:mainWindow];
//        [Factory imageViewWithFrame:CGRectMake(0, 00, __MAIN_WIDTH, 150*__MAIN_RATIO_375) image:ImageNamed(@"carFenceNew") onView:view];
//        [Factory imageViewWithCenter:CGPointMake(__MAIN_WIDTH/2, __MAIN_HEIGHT / 2 + 60*__MAIN_RATIO_320) image:ImageNamed(@"tips_first_ok") onView:view];
//        
//        [Factory buttonEmptyWithFrame:CGRectMake(60, __MAIN_HEIGHT / 2, __MAIN_WIDTH - 120, 120) click:^{
//            [view removeFromSuperview];
//            [AISystemInfo sharedInstance].shouldShowCarFenceNewTips = NO;
//        } onView:view];
//    }
    
}

-(void)addAddTips{
    __weak __typeof(self) wself = self;
    [TipsFactory addAddTipsOnView:self.view tips:@"点击添加安全区域" click:^{
        CarFenceDetailViewController *vc = [[[CarFenceDetailViewController alloc] init] setupNavBack];
        [wself.navigationController pushViewController:vc animated:YES];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    __weak __typeof(self) wself = self;
    [CarFenceMgr updateFencesFinish:^{
        if ([CarFenceMgr sharedInstance].fences.count > 0) {
            [wself.tableView reloadData];
            [TipsFactory removeAddTipsOnView:wself.view];
        }else{
            [wself addAddTips];
        }
    }];
}

#pragma mark - 
-(void)loadMoreTopFinish:(void (^)(CGFloat))finish{
    __weak __typeof(self) wself = self;
    [CarFenceMgr updateFencesFinish:^{
        [wself.tableView reloadData];
        if ([CarFenceMgr sharedInstance].fences.count == 0) {
            [wself addAddTips];
        }else{
            [TipsFactory removeAddTipsOnView:wself.view];
        }
        if (finish) {
            finish(0);
        }
    }];
}

#pragma mark - tableView

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return [CarFenceMgr sharedInstance].fences.count;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return [CarFenceCell cellHeight];
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    static NSString *cellID = @"cell";

    CarFenceCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[CarFenceCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }
    CarFenceModel *model = [CarFenceMgr sharedInstance].fences[indexPath.row];
    cell.model = model;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CarFenceModel *model = [CarFenceMgr sharedInstance].fences[indexPath.row];
    CarFenceDetailViewController *vc = [[[CarFenceDetailViewController alloc] initWithFence:model] setupNavBack];
    [self.navigationController pushViewController:vc animated:YES];
}

-(BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath{
    return [CarFenceMgr sharedInstance].fences.count > 0;
}

-(NSString *)tableView:(UITableView *)tableView titleForDeleteConfirmationButtonForRowAtIndexPath:(NSIndexPath *)indexPath{
    return @"删除";
}

-(void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath{
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        if (![Check isVisiter]) {
            CarFenceModel *model = [CarFenceMgr sharedInstance].fences[indexPath.row];
            __weak __typeof(self) wself = self;
            [CarFenceMgr removeFence:model finish:^{
                if ([CarFenceMgr sharedInstance].fences.count == 0) {
                    [wself.tableView reloadData];
                    [wself addAddTips];
                }else{
                    [wself.tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
                }
            }];
        }
    }
}

@end
